package com.jk.libsm.controller;

import com.alibaba.fastjson.JSONObject;
import com.jk.libsm.service.AdminService;
import com.jk.libsm.service.UserService;
import com.jk.libsm.vo.Bookinfo;
import com.jk.libsm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.security.acl.LastOwnerException;

/**
 * @author lk
 * @date 2020/7/6 0006 15:04
 * @description:
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    UserService userService;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.get("id").toString();
        String password = jsonObject.get("password").toString();
        return adminService.adminLogin(id, password);
    }

    //增加图书
    @PostMapping("/add_books")
    public Result addBooks(@RequestBody JSONObject jsonObject) {
        String token = jsonObject.get("token").toString();
        String book_name = jsonObject.get("book_name").toString();
        String book_author = jsonObject.get("book_author").toString();
        String book_type = jsonObject.get("book_type").toString();
        String book_press = jsonObject.get("book_press").toString();
        float book_price = Float.parseFloat(jsonObject.get("book_price").toString());
        String book_intro = jsonObject.get("book_intro").toString();
        String position = jsonObject.get("position").toString();
        int book_num = Integer.parseInt(jsonObject.get("book_num").toString());
        Bookinfo bookinfo = new Bookinfo();
        bookinfo.setBook_num(book_num);
        bookinfo.setToken(token);
        bookinfo.setBook_name(book_name);
        bookinfo.setBook_author(book_author);
        bookinfo.setBook_type(book_type);
        bookinfo.setBook_press(book_press);
        bookinfo.setBook_price(book_price);
        bookinfo.setBook_intro(book_intro);
        bookinfo.setPosition(position);
        return adminService.adminAddBooks(bookinfo);
    }

    //按条件查询图书
    @PostMapping("/find_books_by")
    public Result findBooksBy(@RequestBody JSONObject jsonObject) {
        String token = jsonObject.get("token").toString();
        int type = Integer.parseInt(jsonObject.get("type").toString());
        String info = jsonObject.get("info").toString();
        return adminService.adminFindBooksBy(token, type, info);
    }

    //按编号删除图书
    @PostMapping("/delete")
    public Result delete(@RequestBody JSONObject jsonObject) {
        String token = jsonObject.get("token").toString();
        int book_id =Integer.parseInt(jsonObject.get("book_id").toString());
        return adminService.adminDelete(token, book_id);
    }

    //查询全部用户
    @PostMapping("/find_all_users")
    public Result findAllUsers(@RequestBody JSONObject jsonObject) {
        String token = jsonObject.get("token").toString();
        return adminService.adminFindAllUsers(token);
    }

    //按用户名查询特定用户
    @PostMapping("/find_user_by_id")
    public Result findUserById(@RequestBody JSONObject jsonObject) {
        String token = jsonObject.get("token").toString();
        String id = jsonObject.get("id").toString();
        return adminService.adminFindUserById(token, id);
    }

    //更改用户权限
    @PostMapping("/update_user_auth")
    public Result updateUserAuth(@RequestBody JSONObject jsonObject) {
        String token = jsonObject.get("token").toString();
        String id = jsonObject.get("id").toString();
        int state = Integer.parseInt(jsonObject.get("state").toString());
        return adminService.adminUpdateUserAuth(token, id, state);
    }


    //管理员注册
    @PostMapping("/regist")
    public Result regist(@RequestBody JSONObject jsonObject) {
        String token = jsonObject.get("token").toString();
        String id = jsonObject.get("id").toString();
        String password = jsonObject.get("password").toString();
        String name = jsonObject.get("name").toString();
        String phone_num = jsonObject.get("phone_num").toString();
        return adminService.adminRegist(token, id, password, name, phone_num);
    }

    //查询特定用户的预约状态
    @PostMapping("/find_order_by_id")
    public  Result findOrderStateById(@RequestBody JSONObject jsonObject){
        String token = jsonObject.get("token").toString();
        String id = jsonObject.get("id").toString();
        return  userService.userFindOrderState(id);
    }

    //管理员给用户借书
    @PostMapping("/borrow_book")
    public  Result borrowBook(@RequestBody JSONObject jsonObject){
        String token = jsonObject.get("token").toString();
        int book_id = Integer.parseInt(jsonObject.get("book_id").toString());
        String id = jsonObject.get("id").toString();
        return  adminService.adminBorrowBook(token,id,book_id);
    }

    //还书
    @PostMapping("/return_book")
    public  Result returnBook(@RequestBody JSONObject jsonObject){
        String token = jsonObject.get("token").toString();
        String id = jsonObject.get("id").toString();
        int book_id = Integer.parseInt(jsonObject.get("book_id").toString());
        return  adminService.adminReturnBook(token,id,book_id);
    }
}
